package com.clay.appointment.controller;

import com.alibaba.fastjson.JSON;
import com.clay.appointment.common.response.Result;
import com.clay.appointment.entity.dict.Dict;
import com.clay.appointment.service.DictService;
import com.clay.appointment.utils.IpUtils;
import com.clay.appointment.utils.RequestContextUtils;
import com.clay.appointment.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : clay
 * @Description:
 * @Date: Create in 21:48 2021/7/25
 * @Modified By:
 */
@Api("数据字典管理")
@RestController
@RequestMapping(value = "/dict")
public class DictController {
    private final DictService dictService;

    public DictController(DictService dictService) {
        this.dictService = dictService;
    }


    @ApiOperation("根据数据id查询子数据列表")
    @PostMapping("/findChildrenData/{id}")
    public Result<List<Dict>> findChildrenData(@PathVariable(name = "id") Long id) {
        return Result.success(dictService.findChildrenData(id));
    }


    @ApiOperation("导出数据字典")
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exportDictData(response);
    }


    @ApiOperation("导入数据字典")
    @PostMapping("importData")
    public Result importDict(MultipartFile file) {
        dictService.importDictData(file);
        return Result.success();
    }


    @ApiOperation(value = "根据上级编码和值获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

    @ApiOperation(value = "根据值获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("", value);
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable("dictCode") String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.success(list);
    }

//    @ApiOperation(value = "根据dictCode获取下级节点")
//    @GetMapping(value = "/findByDictCode1/{dictCode}")
//    public Result findByDictCode1(
//            @ApiParam(name = "dictCode", value = "节点编码", required = true)
//            @PathVariable("dictCode") String dictCode) {
//        dictService.findByDictCode1(dictCode);
//        return Result.success();
//    }

    @Resource
    RedisTemplate<String, Dict> redisTemplate;

    @GetMapping(value = "/redisTemplate")
    public Result<Object> redisTemplate() {
        //登录
        Dict dict = new Dict();
        dict.setId(1L);
        Map<String, String> map = new HashMap<>();
        map.put("id", "1");
        map.put("name", "tony");
        map.put("ip", IpUtils.getIpAddr(RequestContextUtils.getRequest()));
        String token = TokenUtils.encode(map);
        ValueOperations<String, Dict> opsForValue = redisTemplate.opsForValue();
        opsForValue.set(token, dict);

        //验证 RSA
        Map<String, String> decode = TokenUtils.decode(token);
        System.out.println(JSON.toJSONString(decode));
        System.out.println(decode.get("ip").equals(IpUtils.getIpAddr(RequestContextUtils.getRequest())));

        Dict dict1 = opsForValue.get(token);

        return Result.success(dict1);


    }
}
